using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MES_workbench.Api.DTO;
using MES_workbench.Api.Implementationlayer.IServices;
using MES_workbench.Domian.Entities;
using MES_workbench.ErrorCode;

namespace MES_workbench.Api.Controllers
{
    /// <summary>
    /// 工作台控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WorkbenchController : ControllerBase
    {
        private readonly ILogger<WorkbenchController> logger;
        private readonly IWorkbenchServices workbenchServices;

        public WorkbenchController(ILogger<WorkbenchController> _logger, IWorkbenchServices workbenchServices)
        {
            logger = _logger;
            this.workbenchServices = workbenchServices;
        }

        /// <summary>
        /// 查询数据统计（最近7天/最近1个月/最近1年）
        /// </summary>
        /// <param name="dto">时间范围参数："7d" | "1m" | "1y"</param>
        /// <returns>ApiResult 包装的 DataStatistics 列表</returns>
        [HttpPost("data-statistics")]
        public async Task<ApiResult<List<DataStatistics>>> GetDataStatistics([FromBody] DataStatisticsQueryDto dto)
        {
            logger.LogInformation("[Controller] 接收到数据统计查询请求 Range={Range}", dto?.Range);
            var res = await workbenchServices.GetDataStatisticsAsync(dto);
            return res;
        }


        /// <summary>
        /// 获取公告（无需查询参数），按最新接收时间倒序。
        /// </summary>
        /// <param name="onlyNotDeleted">是否仅返回未删除数据，默认 true</param>
        [HttpGet("notices/latest")]
        public async Task<ApiResult<List<Notice>>> GetLatestNotices([FromQuery] bool onlyNotDeleted = true)
        {
            logger.LogInformation("[Controller] 获取最新公告 onlyNotDeleted={Only}", onlyNotDeleted);
            var res = await workbenchServices.GetLatestNoticesAsync(onlyNotDeleted);
            return res;
        }

        /// <summary>
        /// 获取订单交付提醒（无查询参数），按最新交付时间倒序。
        /// </summary>
        /// <returns>ApiResult 包装的 OrderDeliveryReminder 列表</returns>
        [HttpGet("order-delivery-reminders/latest")]
        public async Task<ApiResult<List<OrderDeliveryReminder>>> GetLatestOrderDeliveryReminders()
        {
            logger.LogInformation("[Controller] 获取最新订单交付提醒（无参）");
            var res = await workbenchServices.GetLatestOrderDeliveryRemindersAsync();
            return res;
        }

        /// <summary>
        /// 获取工单提醒（无查询参数），按计划开始时间倒序。
        /// </summary>
        /// <returns>ApiResult 包装的 WorkOrderReminder 列表</returns>
        [HttpGet("work-order-reminders/latest")]
        public async Task<ApiResult<List<WorkOrderReminder>>> GetLatestWorkOrderReminders()
        {
            logger.LogInformation("[Controller] 获取最新工单提醒（无参）");
            var res = await workbenchServices.GetLatestWorkOrderRemindersAsync();
            return res;
        }

        /// <summary>
        /// 获取工序提醒（无查询参数），按主键ID倒序。
        /// </summary>
        /// <returns>ApiResult 包装的 ProcessStepReminder 列表</returns>
        [HttpGet("process-step-reminders/latest")]
        public async Task<ApiResult<List<ProcessStepReminder>>> GetLatestProcessStepReminders()
        {
            logger.LogInformation("[Controller] 获取最新工序提醒（无参）");
            var res = await workbenchServices.GetLatestProcessStepRemindersAsync();
            return res;
        }

        /// <summary>
        /// 获取“快过期”物品数据（还剩1~2天到期），按到期日倒序。
        /// 窗口：[今天+1天 00:00, 今天+3天 00:00)。
        /// </summary>
        /// <returns>ApiResult 包装的 ItemsApproachingExpiry 列表</returns>
        [HttpGet("items-approaching-expiry/not-expired")]
        public async Task<ApiResult<List<ItemsApproachingExpiry>>> GetItemsNotExpired()
        {
            logger.LogInformation("[Controller] 获取快过期（还剩1~2天到期）的即将到期物品（无参）");
            var res = await workbenchServices.GetItemsNotExpiredAsync();
            return res;
        }

        /// <summary>
        /// 获取即将到期物品中“已过期”的数据（ExpiryDate 小于 当前时间），按到期日倒序。
        /// </summary>
        /// <returns>ApiResult 包装的 ItemsApproachingExpiry 列表</returns>
        [HttpGet("items-approaching-expiry/expired")]
        public async Task<ApiResult<List<ItemsApproachingExpiry>>> GetItemsExpired()
        {
            logger.LogInformation("[Controller] 获取已过期的即将到期物品（无参）");
            var res = await workbenchServices.GetItemsExpiredAsync();
            return res;
        }
    }
}
